package com.chookss.home.audit.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.entity.ReimbursementDetailEntity;
import com.chookss.tools.GridSpacingItemDecoration;
import com.chookss.tools.MyEvent;
import com.chookss.tools.Utils;
import com.chookss.view.DrawableTextView;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.annotations.Nullable;

public class FilesAdapter extends BaseQuickAdapter<ReimbursementDetailEntity.AnnexsBean, BaseViewHolderEx> {
    private Context context;
    private int type; //0可以修改删除1不行只能展示
    private String tag;
    private GridSpacingItemDecoration gridSpacingItemDecoration;
    ReimbursementImagsAdapter picAdapter = null;
    ReimbursementDocunmentListAdapter docunmentAdapter = null;

    public FilesAdapter(int layoutResId, Context context, @Nullable List<ReimbursementDetailEntity.AnnexsBean> data, int type, String tag) {
        super(layoutResId, data);
        this.context = context;
        this.type = type;
        this.tag = tag;
        gridSpacingItemDecoration = new GridSpacingItemDecoration(3, Utils.dip2px(context, 12), false);
    }

    @Override
    protected void convert(BaseViewHolderEx helper, ReimbursementDetailEntity.AnnexsBean item) {
        TextView tvTitle = helper.getView(R.id.tvTitle);
        DrawableTextView tvChoice = helper.getView(R.id.tvChoice);
        RecyclerView picRecyclerView = helper.getView(R.id.picRecyclerView);
        RecyclerView documentRecyclerView = helper.getView(R.id.documentRecyclerView);
        if(tag.equals("ReimbursementDetailActivity_DownloadFile")||tag.equals("ReimbursementActivity_DownloadFile")){
            tvTitle.setText(item.getAnnexName()+"(借款附件)");
        }else if(tag.equals("ReimbursementDetailActivity_reimbursement_DownloadFile")||tag.equals("ReimbursementActivity_reimbursement_DownloadFile")){
            tvTitle.setText(item.getAnnexName()+"(还款附件)");
        }else{
            tvTitle.setText(item.getAnnexName());
        }
        if(type==0){
            tvChoice.setVisibility(View.VISIBLE);
        }else{
            tvChoice.setVisibility(View.GONE);
        }
        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(context, 3);
        picRecyclerView.setLayoutManager(gridLayoutManager1);
        if (picRecyclerView.getItemDecorationCount() == 0) {
            picRecyclerView.addItemDecoration(gridSpacingItemDecoration);
        }
        if (item.getPictureInfoList().size() > 0) {
            picRecyclerView.addItemDecoration(gridSpacingItemDecoration);
            picAdapter = new ReimbursementImagsAdapter(R.layout.item_publish_log, context, item.getPictureInfoList(), helper.getItemPosition(), type);
            picRecyclerView.setAdapter(picAdapter);
            picRecyclerView.setVisibility(View.VISIBLE);
        } else {
            picRecyclerView.setVisibility(View.GONE);
        }
        if (item.getDatabase().size() > 0) {
            documentRecyclerView.setLayoutManager(new LinearLayoutManager(context));
            docunmentAdapter = new ReimbursementDocunmentListAdapter(R.layout.item_file_list, context, item.getDatabase(), helper.getItemPosition(), type, tag);
            documentRecyclerView.setAdapter(docunmentAdapter);
            documentRecyclerView.setVisibility(View.VISIBLE);
        } else {
            documentRecyclerView.setVisibility(View.GONE);
        }
        tvChoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                Map<String, String> map = new HashMap<>();
                map.put("Files_choice_parent_num", helper.getItemPosition() + "");
                EventBus.getDefault().post(new MyEvent(map));
            }
        });
    }

    public void setRefresh(int position) {
        if(docunmentAdapter!=null){
            docunmentAdapter.notifyItemChanged(position);
        }
    }
}
